# coding:utf-8
# @Time      :2022/8/171:48 AM
# @Author    :peiyu
# @FileName  :cart_api
# @email     :geektime
# @Blog      :https://gitee.com/liupeiyu001/geektime_litemall.git
from dataclasses import dataclass

from litemall.api.baseinfo import BaseInfo
from litemall.api.session_api import SessionApi
from litemall.framework.http import Request
from litemall.model.cart import Cart
from litemall.model.goods import Goods
from litemall.model.session import Session


@dataclass
class CartApi(Cart):

    def __init__(self):
        self.session = SessionApi()

    def list(self):
        cart_list = Request()
        cart_list.method = "get"
        cart_list.type = "json"
        cart_list.headers = {"X-Litemall-Token": self.session.refresh_token()}
        cart_list.path = "/wx/cart/index"
        cart_list.host = BaseInfo.host
        r = cart_list.send()
        return r

    def add_goods(self,goods:Goods):
        cart = Request()
        cart.method = "post"
        cart.type = "json"
        cart.data = {"goodsId":goods.goodsid,"number":goods.number,"productId":goods.id}
        cart.headers = {"X-Litemall-Token": self.session.refresh_token()}
        cart.path = "/wx/cart/add"
        cart.host = BaseInfo.host
        r = cart.send()
        return r

    def delete_goods(self,goods:Goods):
        cart = Request()
        cart.method = "post"
        cart.type = "json"
        cart.data = {"productIds":[goods.goodsid]}
        cart.headers = {"X-Litemall-Token": self.session.refresh_token()}
        cart.path = "wx/cart/delete"
        cart.host = BaseInfo.host
        r = cart.send()
        return r
